package GUILayer;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import CtrLayer.*;
import javax.swing.JPasswordField;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import ModelLayer.*;

public class LoginGUI extends JFrame {

	private JFrame frmLogin;
	private JTextField userNameField;
    private static int heightStartWindow;
    private static int widthStartWindow;
    private PersonCtr personCtr;
    private JPasswordField passwordField;
    private Person employee;
    /** Creates new form LoginGUI */


public int getHeightStartWindow()
    {
        return heightStartWindow;
    }

public int getWidthStartWindow()
    {
        return widthStartWindow;
    }

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				Toolkit toolkit =Toolkit.getDefaultToolkit();
				Dimension scrnsize =toolkit.getScreenSize();
				int height = (scrnsize.height-600)/2;
				int width = (scrnsize.width-1050)/2;
				
				heightStartWindow = height;
				widthStartWindow = width;
				
				try {
					LoginGUI loginGUI = new LoginGUI();
					loginGUI.setBounds(width, height, 1050, 600);
					loginGUI.frmLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginGUI() {
		initialize();
		personCtr = personCtr.getInstance();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmLogin = new JFrame();
		frmLogin.setTitle("Login");
		frmLogin.setBounds(100, 100, 214, 206);
		frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmLogin.setJMenuBar(menuBar);
		
		JMenu mnFiler = new JMenu("Filer");
		menuBar.add(mnFiler);
		
		JMenuItem mntmAfslutProgram = new JMenuItem("Afslut program");
		mntmAfslutProgram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnFiler.add(mntmAfslutProgram);
		
		JMenu mnHjlp = new JMenu("Hj\u00E6lp");
		menuBar.add(mnHjlp);
		
		JMenuItem om = new JMenuItem("Om");
		om.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAbout();
			}
		});
		mnHjlp.add(om);
		
		JLabel brugerNavnLabel = new JLabel("Brugernavn:");
		
		JLabel passwordLabel = new JLabel("Kodeord:");
		
		userNameField = new JTextField();
		userNameField.setColumns(10);
		
		JButton quitButton = new JButton("Afslut");
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				stop();
			}
		});
		
		JButton loginButton = new JButton("Login");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logOn();
			}
		});
		
		passwordField = new JPasswordField();
		passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordFieldKeyPressed(evt);
            }
        });
		GroupLayout groupLayout = new GroupLayout(frmLogin.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(passwordField, Alignment.LEADING)
						.addComponent(brugerNavnLabel, Alignment.LEADING)
						.addComponent(passwordLabel, Alignment.LEADING)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(loginButton)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(quitButton))
						.addComponent(userNameField, Alignment.LEADING))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(brugerNavnLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(userNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(passwordLabel)
					.addGap(5)
					.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(loginButton)
						.addComponent(quitButton))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		frmLogin.getContentPane().setLayout(groupLayout);
		frmLogin.setVisible(true);
	}
	@SuppressWarnings("deprecation")
	private void logOn()
	{
		try
		{
		employee = personCtr.logOn(userNameField.getText(), passwordField.getText());
		
			System.out.println("OK");
			MainGUI mainGUI = new MainGUI(userNameField.getText(), this);
			mainGUI.setBounds(heightStartWindow,widthStartWindow,1050,600);
            //mainGUI.frmHovedMenu.setVisible(true);

            userNameField.setText("");
            passwordField.setText("");
            this.setVisible(false);
            frmLogin.setVisible(false);

	}
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            frmLogin = this;
            JOptionPane.showMessageDialog(frmLogin, "Invalid login or password", "Error Message", JOptionPane.ERROR_MESSAGE);
            
            userNameField.setText("");
            passwordField.setText("");
        }
	}
	
	private void stop()
	{
		System.exit(0);
	}
	
	private boolean enterPress(KeyEvent evt)
    {
        boolean enter = false;
        int key = evt.getKeyCode();
        if(key == KeyEvent.VK_ENTER)
        {
            enter = true;
        }
        return enter;
    }
	private void passwordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginPasswordFieldKeyPressed
        if(enterPress(evt))
        {
            logOn();
        }
	}
	private void showAbout()
	{
		JOptionPane.showMessageDialog(frmLogin, "Vestbjerg Byggecenter\n" + "Version 1.0", "About Vestbjerg Byggecenter", JOptionPane.INFORMATION_MESSAGE);
	}
	private static final long serialVersionUID = 7526472295622776147L;


public Person getEmployee()
{
	return employee;
}
}
